﻿using CommHelper;
using Newtonsoft.Json;
using NX5Web.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace NX5Web.Controllers
{
    public class MPController : BaseApiController
    { 
        public class MPLoginObj
        {
            public String Code { set; get; }
            public Int32 LoginUid { set; get; }
        }

        public class MPLoginResponse
        {
            public String session_key { set; get; }
            public String openid { set; get; }
        }

        // GET api/<controller>/5
        [HttpPost]
        public IHttpActionResult GetOpenId(MPLoginObj User)
        {
            HelpRequest request = new HelpRequest();
            String ScKey = "6a33b006b350980f1ce810a4a246c824";
            String Appid = "wxa5578c8b130b10eb";
            MPLoginResponse MPResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<MPLoginResponse>(request.get("https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid
                + "&secret=" + ScKey
                + "&js_code=" + User.Code
                + "&grant_type=authorization_code"));

            String OpenId = MPResponse.openid;
            DataManager dm = new DataManager();
            dm.SqlCmd("UPDATE System_Accounts SET OPENID = '"+ OpenId + "' WHERE ID = " + User.LoginUid);
            return ReturnSuccess(OpenId);
        } 
    }
}